﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using eCitizen.eAlerts.Utilities;
using eCitizen.eAlerts.Constants;
using eCitizen.DataContracts;
using eCitizen.DataAccess;

namespace eCitizen.eAlerts.BusinessService
{
    public class SendAlertService : ServiceBase,IServiceBase
    {
        
        public void RunService()
        {

            SftpConnectionManager connectionManager = new SftpConnectionManager();
            FileManager requestFileManager = new FileManager();
            RequestFileManager alertManager = null;
            SubscriberManager subscriberManager = new SubscriberManager();
            MessageBroadcastManager broadcastManager = null;

            // Start of the SendAlertService
            CurrentServiceHelper.StartBatchJobWatch(ServiceType.SendAlertService);
            // Start of the SendAlertService

            foreach (SftpConfiguration config in requestFileManager.SftpConfigurations)
            {
                CurrentConfiguration = config;

                //Start logging for the current configuration
                CurrentServiceHelper.StartServiceInstanceWatch(CurrentConfiguration.Agency,CurrentConfiguration.Service);

                //Copy the files from the Sftp Server to local
                connectionManager.TransferRemoteAgencyFilesToLocal(config);

                List<ServiceRequest> requestfiles = requestFileManager.GeteAlertRequestFiles(config);

                foreach (ServiceRequest request in requestfiles)
                {
                    #region "Process Each Request file downloaded"

                    CurrentServiceInfo = request.CurrentServiceInfo;

                    broadcastManager = new MessageBroadcastManager();
                    alertManager = new RequestFileManager();
                    List<RequestEntry> subscriptionRequests = new List<RequestEntry>();

                    // Log request files into the system

                    CurrentServiceHelper.LogeAlertFileToProcess(request.RequestFileName, true, DateTime.Now);
                    CurrentServiceHelper.LogeAlertFileToProcess(request.ChecksumFileName, true, DateTime.Now);

                    bool isParseSuccessful = alertManager.TryParseSendServiceAlerts(request, out subscriptionRequests);

                    if (isParseSuccessful)
                    {

                        // Log the incoming Requests into Db
                        subscriptionRequests = CurrentServiceHelper.LogAlertServiceRequests(subscriptionRequests);

                        // Validate the Requests to ensure that the Message Validity is not expired yet
                        subscriptionRequests = alertManager.FilteredExpiredRequests(subscriptionRequests);

                        // Get the subscriber information based on the incoming Requests
                        List<Subscriber> subscriptionsForBroadcast = subscriberManager.GetSubscribersforBroadcast(subscriptionRequests);

                        // Log the subscribers which matches the Requests
                        CurrentServiceHelper.LogSubscribersForBroadcast(subscriptionsForBroadcast);

                        // Send the Alerts(Email/Sms) to the subscribers
                        broadcastManager.BroadcasteAlertMessages(subscriptionsForBroadcast);
                    }

                    #endregion
                }


                // Parsing the request file failed - Error File will be generated
                // Put the Error Log into the sFTP Server
                connectionManager.TransferLocalErrorLogToAgency(config);

                // Copy OneSms Alert files
                connectionManager.TransferLocalFilesToOneSms(config);

                requestFileManager.ArchiveSendAlertServiceFiles(config);

                CurrentServiceHelper.StopServiceInstanceWatch();
            }

            // End of the SendAlertService
            CurrentServiceHelper.StopBatchJobWatch();
            // End of the SendAlertService
        }



    }
}
